package controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.UserModel;
import service.UserService;
import service.impl.UserServiceImpl;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet/*")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private UserService userservice=new UserServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getPathInfo().substring(1);
		if("findByName".equals(path)){
			findByName(request,response);
		}
		if("findPhoto".equals(path)){
			findPhoto(request,response);
		}
	}

	private void findByName(HttpServletRequest request, HttpServletResponse response) {
		UserModel userModel=new UserModel();
		userModel.setUsername((String) request.getSession().getAttribute("username"));
		String username=userModel.getUsername();
		try {
			List<UserModel> userlist = userservice.findByName(username);
			request.setAttribute("userlist",userlist);
			request.getRequestDispatcher("/userlist1.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private void findPhoto(HttpServletRequest request, HttpServletResponse response) {
		try {
			UserModel userModel=new UserModel();
			userModel.setUsername((String) request.getSession().getAttribute("username"));
			String username=userModel.getUsername();
			List<UserModel> userlist = userservice.findByName(username);
			request.setAttribute("userlist",userlist);
			request.getRequestDispatcher("/userlist5.jsp").forward(request,response);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
